<?php

class bookmarks_ListController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }

  //public timeline
  function publicAction() {
    require_once('bookmarks/objects/bookmark.php');
    try{
      //as it is public timeline, wa can go directly in the status table to get them
	  $bookmarksArray = Bookmarks_Object_Bookmark::listing($this->_getParam('perPage'),$this->_getParam('page'));
    } catch(Bookmarks_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	$data['status'] = 	'OK';
	
	//we can t use status as it is alredy used to say evrything went fine
	$data['page'] = Bookmarks_Object_Bookmark::formatPage($this->_getParam('page'));
	$data['perPage'] = Bookmarks_Object_Bookmark::formatPerPage($this->_getParam('perPage'));	
	$data['bookmarks'] =  $bookmarksArray;	
	
	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;
  } //end create action

  
  
  //USer timeline (what the user published)
  function userAction() {
    require_once('bookmarks/objects/list.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$userId = $this->_getParam('userId');
    	$userPseudo = $this->_getParam('userPseudo');
    	//If user id is empty and pseudo is also empty we take the authenticated user id 
    	if(empty($userId) && empty($userPseudo)) {
    		$oUser = Zend_Registry::get('app_user');
    		$userId = $oUser->getId();
    	}
    	//If the search is using the pseudo, we need to retrieve the identifier of the user
    	if(empty($userId) && !empty($userPseudo)) {
    		try{
				$oUser = new Myzen_User(array('pseudo' => $userPseudo));
				$userId = $oUser->getId();
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}	
    	}

		//We collect the different search parameters
		$beforeBookmarkId = $this->_getParam('beforeBookmarkId');
		$afterBookmarkId = $this->_getParam('afterBookmarkId');		

	
    	//we get the timeline of the user
    	$list = new Bookmarks_Object_List('user',$userId);
		
		//We select the stream per page if $beforeBookmarkId is empty and $afterBookmarkId 
		if(empty($beforeBookmarkId ) && empty($afterBookmarkId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$bookmarks= $list->listingPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeBookmarkId )) {
			$searchMode = 'beforeBookmarkId';
			$bookmarks= $list->listingPerBookmarkId('before',$beforeBookmarkId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterBookmarkId )) {
			$searchMode = 'afterBookmarkId';
			$bookmarks= $list->listingPerBookmarkId('after',$afterBookmarkId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty


	} catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    
    //now we may need to retrieve the user object if we are in the case where the app provided userId
    	if(!isset($oUser)) {
     		try{
				$oUser = new Myzen_User(array('id' => $userId));
			} catch(Zend_Auth_Exception $e) {
				$data['status'] = 	'KO';
				$data['error']['num'] = 	$e->getCode();
				$data['error']['message'] = 	$e->getMessage();
				echo Zend_Registry::get('dt_serializer')->serialize($data);	
				return;    		
			}   		
    	}
    

    
    
    //now we can create the message
    $data['status']= 'OK';
    $data['user']= $oUser->asArray();
  	

	$data['mode'] = $searchMode;
	$data['perPage'] = Bookmarks_Object_List::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Bookmarks_Object_List::formatPage($this->_getParam('page'));
	if('beforeBookmarkId' == $searchMode) $data['queryStatusId'] = $beforeBookmarkId;
	if('afterBookmarkId' == $searchMode) $data['queryStatusId'] = $afterBookmarkId;
	
	$data['bookmarks'] =  $bookmarks; 	

    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end  user


  
  
  
  
  
  
  
  
    //Tag timeline
  function tagAction() {
    require_once('bookmarks/objects/list.php');
    try{
    	//If the id is not provided, we return the ones of the identified user
    	$tagId = $this->_getParam('tagId');
    	if(empty($tagId)) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	72;
			$data['error']['message'] = 'Tag is mandatory';
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;			
		}
		
		//We collect the different search parameters
		$beforeBookmarkId = $this->_getParam('beforeBookmarkId');
		$afterBookmarkId = $this->_getParam('afterBookmarkId');
		
    	$list = new Bookmarks_Object_List('tag',$tagId);

		//Statuses search depends on the mode. If the mode is not empty, we search based on the status id
		//if empty we search base on the page
		
		//We select the stream per page if $beforeBookmarkId is empty and $afterBookmarkId 
		if(empty($beforeBookmarkId ) && empty($afterBookmarkId)) {
			//search mode is page, we store it for the return message
			$searchMode = 'page';
			$bookmarks= $list->listingPerPage($this->_getParam('perPage'),$this->_getParam('page'));
		} elseif(!empty($beforeBookmarkId )) {
			$searchMode = 'beforeBookmarkId';
			$bookmarks= $list->listingPerBookmarkId('before',$beforeBookmarkId,$this->_getParam('perPage'));
		} // end beforestatus is not empty
		elseif(!empty($afterBookmarkId )) {
			$searchMode = 'afterBookmarkId';
			$bookmarks= $list->listingPerBookmarkId('after',$afterBookmarkId,$this->_getParam('perPage'));			
		} // end beforestatus is not empty
		
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }   
    
    $data['status']= 'OK';


	$data['mode'] = $searchMode;
	$data['perPage'] = Bookmarks_Object_List::formatPerPage($this->_getParam('perPage'));	
	if('page' == $searchMode) $data['page'] = Bookmarks_Object_List::formatPage($this->_getParam('page'));
	if('beforeBookmarkId' == $searchMode) $data['queryStatusId'] = $beforeBookmarkId;
	if('afterBookmarkId' == $searchMode) $data['queryStatusId'] = $afterBookmarkId;
	
	$data['bookmarks'] =  $bookmarks;    
    
 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end
  
  
  
} //End class


?>